package com.fjsh.base.config.service;

import javax.sql.DataSource;

import com.fjsh.consts.CacheConstants;
import com.fjsh.consts.SecurityConstants;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

/**
 * 重写原生方法支持redis缓存
 */
public class RedisClientDetailsServiceImpl extends JdbcClientDetailsService {

    public RedisClientDetailsServiceImpl(DataSource dataSource) {
        // 数据源
        super(dataSource);
        // 重写查询sql语句按client_id查询
        super.setSelectClientDetailsSql(SecurityConstants.DEFAULT_SELECT_STATEMENT);
        //重写查询语句按默认查询全部
        super.setFindClientDetailsSql(SecurityConstants.DEFAULT_FIND_STATEMENT);
    }

    /**
     * 将客户端详情信息缓存
     * @param clientId
     * @return
     */
    @Override
    @Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
    public ClientDetails loadClientByClientId(String clientId) {
        return super.loadClientByClientId(clientId);
    }
}
